#include <QDebug>

#include <libkydiagnostics.h>

#include "common.h"
#include "information_collector.h"

InformationCollector::InformationCollector(/* args */)
{

}

InformationCollector::~InformationCollector(/* args */)
{

}
void InformationCollector::addPoint(P point) const
{
    QString str;
    switch (point)
    {
    case InformationCollector::P::ManuallyInstallPrinter : str = "ManuallyInstallPrinter"; break;
    case InformationCollector::P::SetResolution          : str = "SetResolution";          break;
    case InformationCollector::P::SetPaperSize           : str = "SetPaperSize";           break;
    case InformationCollector::P::SetPaperType           : str = "SetPaperType";           break;
    case InformationCollector::P::SetCationSource        : str = "SetCationSource";        break;
    case InformationCollector::P::SetDuplexPrint         : str = "SetDuplexPrint";         break;
    case InformationCollector::P::SetInkType             : str = "SetInkType";             break;
    case InformationCollector::P::ADDRemovePrinter       : str = "ADDRemovePrinter";       break;
    case InformationCollector::P::SetShareStartup        : str = "SetShareStartup";        break;
    case InformationCollector::P::PrintTestPage          : str = "PrintTestPage";          break;
    case InformationCollector::P::CancelPrintJob         : str = "CancelPrintJob";         break;
    case InformationCollector::P::DeletePrintJob         : str = "DeletePrintJob";         break;
    case InformationCollector::P::RePrint                : str = "RePrint";                break;
    case InformationCollector::P::ManualyModifyDrive     : str = "ManualyModifyDrive";     break;
    case InformationCollector::P::Rename                 : str = "Rename";                 break;
    default:
        return;
    }
    sendMessage("point", str);
}

void InformationCollector::addMessage(const QString &str) const
{
    if (str.isEmpty()) {
        return ;
    }
    sendMessage("message", str);
}

void InformationCollector::sendMessage(const QString &key, const QString &value) const
{
    if (value.size() > 5000) {
        qDebug() << "InformationCollector::sendMessage value too long !" << value;
    }
    KBuriedPoint p[1];
    p[0].key = QTC(key);
    p[0].value = QTC(value);

    try {
        if (kdk_buried_point(const_cast<char *>("kylin-printer"), const_cast<char *>("FunctionType"), p, 1)) {
            qWarning() << "InformationCollector::sendMessage : kdk_buried_point failed!";
        }
    } catch(const std::exception& e) {
        qDebug() << e.what();
    }
}

#ifdef TEST_DEBUG
const char *test_qtc(const QString &qstr)
{
    if (qstr.isEmpty())
        return nullptr;
    QByteArray ba = qstr.toLatin1();
    return ba.data();
}

void InformationCollector::test() const
{
    KBuriedPoint p[1];
    p[0].key = "test";
    QString value = QString(100000, 'a');
    p[0].value = test_qtc(value);
    try {
        kdk_buried_point(const_cast<char *>("kylin-printer"), const_cast<char *>("FunctionType"), p, 1);
    } catch(const std::exception& e)
    {
        qDebug() << e.what();
    }
}
#endif
